<!--
   Copyright 2024 The Google Research Authors.
  
   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at
  
       http://www.apache.org/licenses/LICENSE-2.0
  
   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
-->

<!DOCTYPE html>
<html>
<head>
<title>Enter Date Login User Task</title>
<!-- stylesheets -->
<link rel="stylesheet" type="text/css" href="../core/core.css">
<style>
  #area { margin: 5px; }
  input { margin: 5px 0; width: 120px; }
  .bold { font-weight: bold; }
  input[id="password"] { width: 120px; }
  input[id="username"] { width: 120px; }
  /* 160 + 80 + 50 + 10 */
  #wrap,
  #sync-task-cover {
    height: 300px;
    width: 160px;
  }

  #click-canvas,
  #reward-display {
    left: 165px;
  }

  #query {
    background-color: #FF0;
    font-size: 10px;
    height: 54px;
    padding: 3px;
  }
</style>

<!-- JS -->
<script src="../core/core.js"></script>
<script src="../common/ui_utils.js"></script>
<script src="../core/d3.v3.min.js"></script>

<script>
core.EPISODE_MAX_TIME = 2000000; // set episode interval to 2000 seconds

// since this is just an input and does not require pagination
// like a date picker, expand the date range by a couple of years.
var START_DATE = new Date(2010, 0, 1); // January 1, 2015
var END_DATE = new Date(2019, 11, 31); // December 31, 2016

var genProblem = function() {
  // enter-date
  var d = ui_utils.randomDate(START_DATE, END_DATE);
  d3.select('#tt')[0][0].value = '';

  var q = ui_utils.toDateString(d);

  // login-user
  d3.select('#username')[0][0].value ='';
  d3.select('#password')[0][0].value ='';

  var user = core.sample(ui_utils.FIFTY_NAMES).toLowerCase();
  var password = ui_utils.generateString(2,6)
  // d3.select('#query').html('Enter <span class="bold">' + q + '</span> as the date, and then enter the <span class="bold">username</span> "' + user + '" and the <span class="bold">password</span> "' + password + '" into the text fields and press login.');
  d3.select('#query').html('Enter the <span class="bold">username</span> "' + user + '" and the <span class="bold">password</span> "' + password + '" into the text fields and press login, after entering <span class="bold">' + q + '</span> as the date.');

  // reward awarder
  d3.select('#subbtn').on('click', function(){
    var u = d3.select('#username')[0][0].value;
    var p = d3.select('#password')[0][0].value;
    var r = (u === user && p === password) ? 1.0 : -1.0;
    core.endEpisode(r, r > 0);
  });

  d3.select('#subbtn').on('click', function(){
    // date comes back in the form of "YYYY-MM-DD", so we'll need to rearrange it.
    var t = d3.select('#tt')[0][0].value.split('-');
    var userDate = t[1] + '/' + t[2] + '/' + t[0];
    var date_r = userDate === q ? 1.0 : -1.0;
    console.log('enter-date', date_r, userDate, q);
    var u = d3.select('#username')[0][0].value;
    var p = d3.select('#password')[0][0].value;
    var login_r = (u === user && p === password) ? 1.0 : -1.0;
    console.log('login-user', login_r);
    if (date_r == 1.0) {core.endEpisode(login_r, login_r > 0);} else {core.endEpisode(-1.0);}
  })
}

window.onload = function() {
  core.startEpisode();
}
</script>
</head>
<!-- Base HTML -->
<body>
<div id="wrap">
  <div id="query"></div>
  <div id="area">
    <div id="form">
      <input type="date" id="tt">
      <p><label class="bold">Username</label><input type="text" id="username"></p>
      <p><label class="bold">Password</label><input type="password" id="password"></p>
      <button id="subbtn" class="secondary-action">Login</button>
    </div>
  </div>
</div>
</body>
</html>
